package cn.com.zero.web.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * @author xiongxj
 * @version 1.0.0
 * @Description knif4j文档配置类
 * @createTime 2022/8/16 14:45
 */
@Configuration
@EnableSwagger2WebMvc
@Profile({"dev", "test"})
@ConfigurationProperties(prefix="knife4j")
public class Knife4jConfig {

    /** 文档版本号 */
    @Value("${knife4j.version:v1.0.0}")
    private String version;

    /** 文档标题 */
    @Value("${knife4j.title:基于zero框架项目的API接口文档}")
    private String title;

    /** 扫描包路径 */
    @Value("${knife4j.base-package:cn.com.zero}")
    private String basePackage;

    /** 文档描述 */
    @Value("${knife4j.description:zero project RESTful APIs}")
    private String description;

    /** 联系人地址 */
    @Value("${knife4j.contact-url:http://www.gnnt.com.cn/we/}")
    private String contactUrl;

    /** 联系人姓名 */
    @Value("${knife4j.contact-name:gnnt}")
    private String contactName;

    /** 联系人邮箱 */
    @Value("${knife4j.contact-email:xuejt@gnnt.com.cn}")
    private String contactEmail;

    /** 服务URL */
    @Value("${knife4j.service-url:http://www.gnnt.com.cn/we/}")
    private String serviceUrl;

    @Bean
    public Docket dockerBean() {
        // 指定使用Swagger2规范
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        // 文档标题
                        .title(title)
                        // 描述字段 支持Markdown语法
                        .description(description)
                        // 联系人信息
                        .contact(new Contact(contactName, contactUrl, contactEmail))
                        // 服务URL
                        .termsOfServiceUrl(serviceUrl)
                        // 版本号
                        .version(version)
                        .build())
                .select()
                // 指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage(basePackage))
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) //只扫描有api注解的类
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //只扫描有ApiOperation注解的方法
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBasePackage() {
        return basePackage;
    }

    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getContactUrl() {
        return contactUrl;
    }

    public void setContactUrl(String contactUrl) {
        this.contactUrl = contactUrl;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getContactEmail() {
        return contactEmail;
    }

    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }

    public String getServiceUrl() {
        return serviceUrl;
    }

    public void setServiceUrl(String serviceUrl) {
        this.serviceUrl = serviceUrl;
    }
}
